﻿using System;
using System.Collections.Generic;
using System.Text;
using EnCor.ObjectBuilder;

namespace EnCor.ModuleLoader
{
    public class ServiceComponentFactory
    {
        public object BuildComponent(ServiceComponentConfig config)
        {
            AssemblerAttribute assemblerAttribute = Attribute.GetCustomAttribute(config.DataType, typeof(AssemblerAttribute)) as AssemblerAttribute;
            IAssembler<object, ServiceComponentConfig> assembler = null;
            if ( assemblerAttribute != null )
            {
                assembler = (IAssembler<object, ServiceComponentConfig>)Activator.CreateInstance(assemblerAttribute.AssemblerType);
            }
            else
            {
                assembler = (config.PolymorphicInstance as ServiceComponentConfig).GetAssembler();
            }
            return assembler.Assemble(null, config.PolymorphicInstance as ServiceComponentConfig);
        }
    }
}
